package com.darrenchan.leetcode;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2019-12-28 16:36
 */
public class Q27 {
    public int removeElement(int[] nums, int val) {
        int lastIndex = nums.length - 1;
        int i = 0;
        while (i <= lastIndex) {
            if (nums[i] == val) {
                nums[i] = nums[lastIndex];
                lastIndex--;
            } else {
                i++;
            }
        }
        return lastIndex + 1;
    }

    public static void main(String[] args) {
        System.out.println(new Q27().removeElement(new int[]{0, 1, 2, 2, 3, 0, 4, 2}, 2));
        System.out.println(new Q27().removeElement(new int[]{2, 1}, 1));
        System.out.println(new Q27().removeElement(new int[]{2, 1, 1}, 1));
    }
}
